package com.xx.dao;

import com.xx.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class JdbcImpl implements Jdbcdao{
    Connection connection = JdbcUtils.getConnection();
    @Override
    public int addUser(User user) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into user(username,`password`,sex,likes,`group`) values (?,?,?,?,?)");
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getSex());
            preparedStatement.setString(4,user.getLikes().get(0));
            preparedStatement.setString(5,user.getGroup());
            if(preparedStatement.executeUpdate() > 0){
                System.out.println("添加用户成功,用户信息为:" + user);
                return 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }return 0;
    }

    @Override
    public int delUser(int id) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("delete from user where id = ?");
            preparedStatement.setInt(1,id);
            if(preparedStatement.executeUpdate() > 0){
                System.out.println("删除用户成功,用户id为:" + id);
                return 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }return 0;
    }

    @Override
    public int updateUser(User user) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("update user set username = ? ,`password` = ? ,sex = ?,likes = ?,`group` = ?where id = ?");
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getSex());
            preparedStatement.setString(4,user.getLikes().get(0));
            preparedStatement.setString(5,user.getGroup());
            preparedStatement.setInt(6,user.getId());
            if(preparedStatement.executeUpdate() > 0){
                System.out.println("更新用户成功,新的用户信息为:" + user);
                return 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }return 0;
    }

    @Override
    public ArrayList<User> selectAll() {
        ArrayList<User> Userlist = new ArrayList<User>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from user");
            ResultSet resultSet = preparedStatement.executeQuery();

            while(resultSet.next()){
                int id = resultSet.getInt(1);
                String username = resultSet.getString(2);
                String password = resultSet.getString(3);
                String sex = resultSet.getString(4);
                String likes = resultSet.getString(5);
                String group = resultSet.getString(6);
                ArrayList<String> list = new ArrayList<String>();
                String[] split = likes.split("#");
                for (String s : split) {
                    list.add(s);
                }
                Userlist.add(new User(id,username,password,sex,list,group));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }return Userlist;
    }
}
